﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form5 : Form
    {
        public Form5()
        {
            InitializeComponent();
            this.btnOK.Enabled = false;

            this.txtAdress.Tag = false;
            this.txtAge.Tag = false;
            this.txtName.Tag = false;
            this.txtOutput.Tag = false;

            this.txtName.Validating += new System.ComponentModel
                .CancelEventHandler(this.txtEmpty_Validating);
            this.txtAdress.Validating += new System.ComponentModel
                .CancelEventHandler(this.txtEmpty_Validating);
            this.txtAge.Validating += new System.ComponentModel
                .CancelEventHandler(this.txtEmpty_Validating);
            this.txtAge.KeyPress += new System.Windows.Forms
                .KeyPressEventHandler(this.txtAge_KeyPress);

        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            string output;

            output = "Name: " + this.txtName.Text + "\r\n";
            output += "Address: " + this.txtAdress.Text + "\r\n";
            output += "Occupation: " + (string)(this.checkBoxProgrammer.Checked ? "Programmer" : "Not a programmer") + "\r\n";
            output += "Sex: " + (string)(this.radioButtonMale.Checked ? "Male" : "Female") + "\r\n";
            output += "Age: " + this.txtAge.Text + "\r\n";

            this.txtOutput.Text = output;
        }

        private void btnHelp_Click(object sender, EventArgs e)
        {
            string output;

            output = "Name = Your name \r\n";
            output += "Adress = Your adress \r\n";
            output += "Programmer = Check 'Programmer' if you are a programmer \r\n";
            output += "Sex = Choose your sex\r\n";
            output += "Age = Your age";

            this.txtOutput.Text = output;
        }

        private void txtEmpty_Validating(object sender, CancelEventArgs e)
        {
            TextBox tb = (TextBox)sender;
            if (tb.Text.Length == 0)
            {
                tb.BackColor = Color.Red;
                tb.Tag = false;
            }
            else
            {
                tb.BackColor = System.Drawing.SystemColors.Window;
                tb.Tag = true;
            }

            ValidateOK();
        }

        private void txt_TextChanged(object sender, EventArgs e)
        {
            TextBox tb = (TextBox)sender;

            if (tb.Text.Length == 0)
            {
                tb.Tag = false;
                tb.BackColor = Color.Red;
            }
            else
            {
                tb.Tag = true;
                tb.BackColor = SystemColors.Window;
            }

            ValidateOK();
        }

        private void txtAge_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8)
                e.Handled = true;
        }

        private void ValidateOK()
        {
            this.btnOK.Enabled = ((bool)(this.txtAdress.Tag) &&
                                 (bool)(this.txtAge.Tag) &&
                                 (bool)(this.txtName.Tag));
        }
    }
}
